package org.lanqiao.service.impl;

import java.util.List;


import org.lanqiao.entity.Priv;
import org.lanqiao.entity.Role;
import org.lanqiao.mapper.PrivMapper;
import org.lanqiao.mapper.RoleMapper;
import org.lanqiao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
@Autowired
	RoleMapper roleMapper;
@Autowired
PrivMapper privMapper;

	public List<Role> getAllRoles() {
		List<Role> list = roleMapper.getAllRoles();
		for(Role role:list) {
			List<Priv> lp = privMapper.selectPrivsByRid(role.getId());
			role.setLp(lp);
		}
		return list;
	}

	public boolean insertRole(Role role) {
		int i = roleMapper.insertRole(role);
		Role role2 = roleMapper.selectRoleByname(role.getRname());
		role.setId(role2.getId());
		int j = roleMapper.insertRolePriv(role);
		return i>0 && j>0;
	}

	public boolean deleteRole(Integer rid) {
		List<Role> list = roleMapper.selectAdminByRid(rid);
		if(list != null) {
			int i = roleMapper.deleteRolePrivByRid(rid);
			int j = roleMapper.deleteRoleByRid(rid);
			return i > 0 && j > 0;
		}
		return false;
	}

	public Role getRoleByRid(Integer rid) {
		Role role = roleMapper.selectRoleByRid(rid);
		List<Priv> lp = privMapper.selectPrivsByRid(rid);
		role.setLp(lp);
		return role;
	}

	public boolean updateRole(Role role) {
		int i = roleMapper.updateRole(role);
		int j = roleMapper.deleteRolePrivByRid(role.getId());
		int k = roleMapper.insertRolePriv(role);
		
		return i > 0 && j > 0 && k > 0;
	}
	}

